﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using CmMvc.Models;

namespace CmMvc.Controllers
{ 
    public class CmEventController : Controller
    {
        private CmContext db = new CmContext();

        private Account GetCurrentUser()
        {
            MembershipUser user = null;

            try
            {
                user = Membership.GetUser();
            }
            catch (ArgumentException)
            {
                return null;
            }

            if (null == user)
                return null;
            else 
                return db.Accounts.Where(
                    a => 0 == String.Compare(a.Email, user.Email, true)).ToList()[0];
        }

        private bool CheckCurrentUserEventPermission(int id, ref CmEvent ev)
        {
            Account acc = GetCurrentUser();
            if (null == acc)
                return false;

            ev = db.CmEvents.Find(id);
            if (null == ev)
                return false;

            if (0 != String.Compare(ev.ApiKey, acc.ApiKey, true))
                return false;

            return true;
        }

        //
        // GET: /CmEvent/

        [Authorize]
        public ViewResult Index()
        {
            Account acc = GetCurrentUser();

            return View(db.CmEvents.Where(
                e => 0 == String.Compare(e.ApiKey, acc.ApiKey, true)).ToList());
        }

        //
        // GET: /CmEvent/Details/5

        [Authorize]
        public ViewResult Details(int id)
        {
            CmEvent cmevent = null;

            if (false == CheckCurrentUserEventPermission(id, ref cmevent))
                return View();

            return View(cmevent);
        }

        //
        // GET: /CmEvent/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /CmEvent/Create

        [HttpPost]
        public ActionResult Create(CmEvent cmevent)
        {
            if (ModelState.IsValid)
            {
                if (null == cmevent ||
                    null == cmevent.ApiKey ||
                    null == cmevent.Data ||
                    null == cmevent.MachineName ||
                    null == cmevent.Source)
                {
                    return View(cmevent);
                }

                //
                // Validate the API key
                //

                Account acc = db.Accounts.First<Account>(
                    a => (0 == String.Compare(cmevent.ApiKey, a.ApiKey, true)));
                if (null == acc)
                    return View(cmevent);

                //
                // Insert the item
                //

                cmevent.Timestamp = DateTime.Now;
                db.CmEvents.Add(cmevent);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(cmevent);
        }

        //
        // GET: /CmEvent/Delete/5
 
        [Authorize]
        public ActionResult Delete(int id)
        {
            CmEvent cmevent = null;

            if (false == CheckCurrentUserEventPermission(id, ref cmevent))
                return View();

            return View(cmevent);
        }

        //
        // POST: /CmEvent/Delete/5

        [Authorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            CmEvent cmevent = null;

            if (false == CheckCurrentUserEventPermission(id, ref cmevent))
                return RedirectToAction("Index");

            db.CmEvents.Remove(cmevent);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}